package org.huang.order.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import lombok.extern.slf4j.Slf4j;
import org.huang.exception.BizException;
import org.huang.order.api.OrderService;
import org.huang.order.dao.OrderMapper;
import org.huang.order.entity.Order;
import org.huang.order.vo.OrderVo;
import org.huang.user.api.UserService;
import org.huang.user.entity.User;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @描述：OrderServiceImpl类描述
 * @作者: huanganwei
 * @创建时间: 2018-08-09 11:23:34
 * @版本: 1.0
 */
@Slf4j
@Service(version = "1.0.0")
public class OrderServiceImpl implements OrderService {


    @Reference(version = "1.0.0")
    private UserService userService;

    @Autowired
    private OrderMapper orderMapper;


    @Override
    public boolean addOrder(OrderVo orderVo) {
        //远程调用接口
        User user = userService.getUserById(orderVo.getUserId());
        if(user == null)
            throw new BizException("用户信息不存在");
        Order order = new Order();
        order.setUserId(user.getId());
        order.setOrderNo(UUID.randomUUID().toString());
        order.setRealName(user.getRealName());
        order.setPhone(user.getPhone());

        order.setCreateTime(new Date());
        order.setState("0");

        order.setGoodsName(orderVo.getGoodsName());
        order.setCount(orderVo.getCount());
        order.setPrice(orderVo.getPrice());
        order.setAllPrice(orderVo.getPrice().multiply(new BigDecimal(orderVo.getCount())));

        orderMapper.insertSelective(order);

        return true;
    }

    @Override
    public List<Order> listByUserId(Integer userId)  {
        return orderMapper.selectByUserId(userId);
    }

    @Override
    public Order getById(Integer id) {
        return orderMapper.selectByPrimaryKey(id);
    }
}
